﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MinistryStandardPlatformBusiness.MessagePool.Entity;
using MinistryStandardPlatformBusiness.MessagePool.QueueSession;
using Newtonsoft.Json.Linq;
using ZeroMQ;

namespace MinistryStandardPlatformBusiness.Helper
{
    class GpsInfoHelper
    {



        /// <summary>
        /// 方向解析
        /// </summary>
        /// <param name="direction"></param>
        /// <returns></returns>
        public static string translateDirection(string direction)
        {
            //正北 正西 正南 正东 西北 西南  东南 东北
            double dir = 0;
            if (!double.TryParse(direction, out dir))
                return "正北";
            if (dir > 22.5 && dir < 90 - 22.5)
            {
                return "东北";
            }
            else if (dir >= 90 - 22.5 && dir <= 90 + 22.5)
            {
                return "正东";
            }
            else if (dir > 90 + 22.5 && dir < 180 - 22.5)
            {
                return "东南";
            }
            else if (dir >= 180 - 22.5 && dir <= 180 + 22.5)
            {
                return "正南";
            }
            else if (dir > 180 + 22.5 && dir < 270 - 22.5)
            {
                return "西南";
            }
            else if (dir >= 270 - 22.5 && dir <= 270 + 22.5)
            {
                return "正西";
            }
            else if (dir > 270 + 22.5 && dir < 360 - 22.5)
            {
                return "西北";
            }
            else if (dir >= 360 - 22.5 && dir <= 360 || dir >= 0 && dir <= 22.5)
            {
                return "正北";
            }
            else
            {
                return "正北";
            }
        }

        public static LatestGpsInfo DeJsonToLatestGpsInfo1(DataTable dtRe)
        {
            #region
            if (dtRe == null)
                return null;
            LatestGpsInfo lg = new LatestGpsInfo();
            try
            {
                lg.VehicleId = dtRe.Rows[0]["vehicleId"].ToString();
                lg.Altitude = dtRe.Rows[0]["altitude"].ToString();
                lg.DevSpeed = dtRe.Rows[0]["devSpeed"].ToString();
                lg.Direction = dtRe.Rows[0]["direction"].ToString();
                lg.Distance = dtRe.Rows[0]["mileage"].ToString();
                lg.GpsDistance = dtRe.Rows[0]["gpsmileage"].ToString();
                lg.Latitude = dtRe.Rows[0]["latitude"].ToString();
                lg.Nlongitude = dtRe.Rows[0]["longitudeoffset"].ToString();
                lg.Nlatitude = dtRe.Rows[0]["latitudeoffset"].ToString();
                lg.Longitude = dtRe.Rows[0]["longitude"].ToString();
                lg.Oil = dtRe.Rows[0]["oilVolumn"].ToString();
                lg.AttachOil = dtRe.Rows[0]["attachOil"].ToString();
                lg.SatelliteNum = "";
                lg.SignalStrength = "";
                lg.SimId = dtRe.Rows[0]["simId"].ToString();
                lg.Speed = dtRe.Rows[0]["speed"].ToString();
                lg.UpTime = dtRe.Rows[0]["recordtime"].ToString();
                lg.WarnSign = dtRe.Rows[0]["warninfo"].ToString();
                lg.Poi = "";
                lg.StatusSign = dtRe.Rows[0]["statusinfo"].ToString();
                lg.vehicleNum = dtRe.Rows[0]["vehicleNum"].ToString();
                lg.battery = dtRe.Rows[0]["battery"].ToString();
            }
            catch (Exception e)
            {
                Log.WriteLog("DeJsonToLatestGpsInfoError", e.ToString());
                lg = null;
            }
            #endregion
            return lg;
        }

        /// <summary>
        /// 解析出合法的gps缓存信息，不合法的信息不包含在返回结果中
        /// </summary>
        /// <param name="dtRe">待解析的gps信息的table</param>
        /// <param name="validSims">合法的gps信息中的sim集合</param>
        /// <returns></returns>
        public static List<LatestGpsInfo> DeJsonToLatestGpsInfoList(DataTable dtRe,out List<string> validSims)
        {
            #region
            validSims = new List<string>();
            if (dtRe == null)
                return null;
            LatestGpsInfo lg = new LatestGpsInfo();
            List<LatestGpsInfo> lgs = new List<LatestGpsInfo>();
            try
            {
                for (int i = 0; i < dtRe.Rows.Count; i++)
                {
                    lg.VehicleId = dtRe.Rows[i]["vehicleId"].ToString();
                    lg.Altitude = dtRe.Rows[i]["altitude"].ToString();
                    lg.DevSpeed = dtRe.Rows[i]["devSpeed"].ToString();
                    lg.Direction = dtRe.Rows[i]["direction"].ToString();
                    lg.Distance = dtRe.Rows[i]["distance"].ToString();
                    lg.GpsDistance = dtRe.Rows[i]["gpsDistance"].ToString();
                    lg.Latitude = dtRe.Rows[i]["latitude"].ToString();
                    lg.Nlongitude = dtRe.Rows[i]["nlongitude"].ToString();
                    lg.Nlatitude = dtRe.Rows[i]["nlatitude"].ToString();
                    lg.Longitude = dtRe.Rows[i]["longitude"].ToString();
                    lg.Oil = dtRe.Rows[i]["attachOil"].ToString();
                    lg.AttachOil = dtRe.Rows[i]["attachOil"].ToString();
                    lg.SatelliteNum = dtRe.Rows[i]["satelliteNum"].ToString();
                    lg.SignalStrength = dtRe.Rows[i]["signalStrength"].ToString();
                    lg.SimId = dtRe.Rows[i]["simId"].ToString();
                    lg.Speed = dtRe.Rows[i]["speed"].ToString();
                    lg.UpTime = dtRe.Rows[i]["uptime"].ToString();
                    lg.WarnSign = dtRe.Rows[i]["warnsign"].ToString();
                    lg.Poi = dtRe.Rows[i]["POI"].ToString();
                    lg.StatusSign = dtRe.Rows[i]["statusSign"].ToString();
                    lg.vehicleNum = dtRe.Rows[i]["vehicleNum"].ToString();
                    lg.battery = dtRe.Rows[i]["battery"].ToString();


                    if (lg.Longitude != "0" && lg.Latitude != "0" && lg.Nlongitude != "0" && lg.Nlatitude != "0")
                    {
                        lgs.Add(lg);
                        validSims.Add(lg.SimId);
                    }
                }
            }
            catch (Exception e)
            {
                Log.WriteLog("DeJsonToLatestGpsInfoError", e.ToString());
            }
            #endregion
            return lgs;

        }

        public static LatestGpsInfo DeJsonToLatestGpsInfo(DataTable dtRe)
        {
            #region
            if (dtRe == null)
                return null;
            LatestGpsInfo lg = new LatestGpsInfo();
            try
            {
                lg.VehicleId = dtRe.Rows[0]["vehicleId"].ToString();
                lg.Altitude = dtRe.Rows[0]["altitude"].ToString();
                lg.DevSpeed = dtRe.Rows[0]["devSpeed"].ToString();
                lg.Direction = dtRe.Rows[0]["direction"].ToString();
                lg.Distance = dtRe.Rows[0]["distance"].ToString();
                lg.GpsDistance = dtRe.Rows[0]["gpsDistance"].ToString();
                lg.Latitude = dtRe.Rows[0]["latitude"].ToString();
                lg.Nlongitude = dtRe.Rows[0]["nlongitude"].ToString();
                lg.Nlatitude = dtRe.Rows[0]["nlatitude"].ToString();
                lg.Longitude = dtRe.Rows[0]["longitude"].ToString();
                lg.Oil = dtRe.Rows[0]["attachOil"].ToString();
                lg.AttachOil = dtRe.Rows[0]["attachOil"].ToString();
                lg.SatelliteNum = dtRe.Rows[0]["satelliteNum"].ToString();
                lg.SignalStrength = dtRe.Rows[0]["signalStrength"].ToString();
                lg.SimId = dtRe.Rows[0]["simId"].ToString();
                lg.Speed = dtRe.Rows[0]["speed"].ToString();
                lg.UpTime = dtRe.Rows[0]["uptime"].ToString();
                lg.WarnSign = dtRe.Rows[0]["warnsign"].ToString();
                lg.Poi = dtRe.Rows[0]["POI"].ToString();
                lg.StatusSign = dtRe.Rows[0]["statusSign"].ToString();
                lg.vehicleNum = dtRe.Rows[0]["vehicleNum"].ToString();
                lg.battery = dtRe.Rows[0]["battery"].ToString();
            }
            catch (Exception e)
            {
                Log.WriteLog("DeJsonToLatestGpsInfoError", e.ToString());
                lg = null;
            }
            #endregion
            return lg;

        }
        public static LatestGpsInfo DeJsonToLatestGpsInfoOld(DataTable dtRe)
        {
            #region
            if (dtRe == null)
                return null;
            LatestGpsInfo lg = new LatestGpsInfo();
            try
            {
                lg.VehicleId = dtRe.Rows[0]["vehicleId"].ToString();
                lg.Altitude = dtRe.Rows[0]["altitude"].ToString();
                lg.DevSpeed = dtRe.Rows[0]["devSpeed"].ToString();
                lg.Direction = dtRe.Rows[0]["direction"].ToString();
                lg.Distance = dtRe.Rows[0]["mileage"].ToString();
                lg.GpsDistance = dtRe.Rows[0]["gpsmileage"].ToString();
                lg.Latitude = dtRe.Rows[0]["latitude"].ToString();
                lg.Nlongitude = dtRe.Rows[0]["longitudeoffset"].ToString();
                lg.Nlatitude = dtRe.Rows[0]["latitudeoffset"].ToString();
                lg.Longitude = dtRe.Rows[0]["longitude"].ToString();
                lg.Oil = dtRe.Rows[0]["oilVolumn"].ToString();
                lg.AttachOil = dtRe.Rows[0]["attachOil"].ToString();
                //lg.SatelliteNum = dt.Rows[0]["satelliteNum"].ToString();
                //lg.SignalStrength = dt.Rows[0]["signalStrength"].ToString();
                lg.SimId = dtRe.Rows[0]["simId"].ToString();
                lg.Speed = dtRe.Rows[0]["speed"].ToString();
                lg.UpTime = dtRe.Rows[0]["inserttime"].ToString();
                lg.WarnSign = dtRe.Rows[0]["warninfo"].ToString();
                lg.Poi = dtRe.Rows[0]["POI"].ToString();
                lg.StatusSign = dtRe.Rows[0]["statusinfo"].ToString();
                lg.vehicleNum = dtRe.Rows[0]["vehicleNum"].ToString();
                lg.battery = dtRe.Rows[0]["battery"].ToString();
            }
            catch (Exception e)
            {
                Log.WriteLog("DeJsonToLatestGpsInfoError", e.ToString());
                lg = null;
            }
            #endregion
            return lg;

        }
        public static bool zmqCellLocationPack(string sim)
        {
            bool reFlag = false;
            if (sim != null && sim != "")
            {
                using (var InsCTX = ZmqContext.Create())
                {
                    using (var InsSockt = InsCTX.CreateSocket(SocketType.REQ))
                    {
                        InsSockt.Connect(UserSession.ClientPubSubPoint);
                        JObject jo = new JObject();
                        JObject joGpsBasic = new JObject();
                        joGpsBasic.Add("simId", sim);
                        jo.Add("GType", "cellLocation");
                        jo.Add("GpsBasic", joGpsBasic);
                        jo.Add("GpsAttatch", sim);
                        ZmqMessage msg = new ZmqMessage();
                        TimeSpan timeOut = new TimeSpan(0, 0, 0, 1, 0);//接收等待超时为1分钟
                        msg.Append(Encoding.UTF8.GetBytes(jo.ToString()));
                        InsSockt.SendMessage(msg);
                        string callBack = InsSockt.Receive(Encoding.UTF8, timeOut);

                        if (callBack.Equals("1"))
                        {
                            reFlag = true;
                            Log.WriteLog("cellLocationOk", jo.ToString());
                        }
                        else
                        {
                            reFlag = false;
                            Log.WriteLog("cellLocationError", jo.ToString());
                        }

                        InsSockt.Disconnect(UserSession.ClientPubSubPoint);
                    }
                }
            }
            return reFlag;
        }
        public static bool zmqGpsAppPack(string sim, string gpsApp)
        {
            bool reFlag = false;
            using (var InsCTX = ZmqContext.Create())
            {
                using (var InsSockt = InsCTX.CreateSocket(SocketType.REQ))
                {
                    InsSockt.Connect(UserSession.ClientPubSubPoint);
                    JObject jo = new JObject();
                    JObject joGpsBasic = new JObject();
                    jo.Add("GType", "gpsApp");
                    jo.Add("GpsBasic", gpsApp);
                    jo.Add("GpsAttatch", sim);
                    ZmqMessage msg = new ZmqMessage();
                    TimeSpan timeOut = new TimeSpan(0, 0, 0, 1, 0);//接收等待超时为1分钟
                    msg.Append(Encoding.UTF8.GetBytes(jo.ToString()));
                    InsSockt.SendMessage(msg);
                    string callBack = InsSockt.Receive(Encoding.UTF8, timeOut);

                    if (callBack.Equals("1"))
                    {
                        reFlag = true;
                        Log.WriteLog("gpsAppOk", jo.ToString());
                    }
                    else
                    {
                        reFlag = false;
                        Log.WriteLog("gpsAppError", jo.ToString());
                    }

                    InsSockt.Disconnect(UserSession.ClientPubSubPoint);
                }
            }
            return reFlag;
        }

    }
}
